
import {
  addRepSmiApi,
  deleteRepSmiApi,
  getListByNameApi,
  modifyRepSmiApi,
  getListNotImiApi,
  modifyRepSmiByImiApi
} from '@/services/packetPush.js';
import { message } from 'ant-design-vue';
export default {
  namespaced: true,
  state: {
    listLoading: false,
    listData: [],
    addRepSmiLoading: false,
    listNotImiData: [],
    listNotImiLoading: false,
    modifyRepSmiByImiLoading: false,
    listDataModel: [],
    paginationOptions: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // table
    async getListByName({ commit, state }, params) {
      commit('setState', { key: 'listLoading', payload: true });
      const { pageNum, pageSize } = state.paginationOptions;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getListByNameApi(apiParams);
      commit('setState', { key: 'listData', payload: rep.data.list });
      commit('setState', { key: 'paginationOptions', payload: { ...state.paginationOptions, total: rep.data.total }});
      commit('setState', { key: 'listLoading', payload: false });
    },
    // 新增
    async addRepSmi({ commit }, params) {
      commit('setState', { key: 'addRepSmiLoading', payload: true });
      try {
        const rep = await addRepSmiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepSmiLoading', payload: false });
      }
    },
    // 删除
    async deleteRepSmi({ commit }, params) {
      try {
        const rep = await deleteRepSmiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      }
    },
    // 修改
    async modifyRepSmi({ commit }, params) {
      commit('setState', { key: 'addRepSmiLoading', payload: true });
      try {
        const rep = await modifyRepSmiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);
          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'addRepSmiLoading', payload: false });
      }
    },
    // 获取报文一级分类下未关联的二级分类列表
    async getListNotImi({ state, commit }, params) {
      commit('setState', { key: 'listNotImiLoading', payload: true });
      const rep = await getListNotImiApi(params);
      commit('setState', { key: 'listNotImiData', payload: rep.data });
      commit('setState', { key: 'listNotImiLoading', payload: false });
    },
    // 修改报文一级分类下未关联的二级分类列表
    async modifyRepSmiByImi({ state, commit }, params) {
      commit('setState', { key: 'modifyRepSmiByImiLoading', payload: true });

      try {
        const rep = await modifyRepSmiByImiApi(params);
        if (rep.data === '成功') {
          message.success(rep.message);

          const rep2 = await getListByNameApi({ name: params.smi });
          commit('setState', { key: 'listDataModel', payload: rep2.data });
          const newItemIndex = state.listData.findIndex(item => item.id === rep2.data?.[0]?.id) ?? -1;
          const na = [...state.listData];
          if (newItemIndex >= 0) {
            na[newItemIndex] = { ... rep2.data[0] };
          }
          commit('setState', { key: 'listData', payload: na });

          return Promise.resolve(true);
        } else {
          message.error(rep.message);
        }
      } catch (error) {
        message.error('操作失败');
      } finally {
        commit('setState', { key: 'modifyRepSmiByImiLoading', payload: false });
      }
    }
  }
};
